﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;


namespace UCOLOR.App.Extend
{
    public class UniversalControl : System.Windows.Controls.Control, System.ComponentModel.ISupportInitialize
    {
        public static readonly DependencyProperty IsBrowserHostedProperty;
        private bool onLoadedInvoked;
        private System.Collections.Generic.Dictionary<object, bool> logicalChildren = new System.Collections.Generic.Dictionary<object, bool>();
        public event System.EventHandler ActualLoaded;
        public event System.EventHandler Initialize;
        public bool IsBrowserHosted
        {
            get
            {
                return (bool)base.GetValue(UniversalControl.IsBrowserHostedProperty);
            }
        }
        protected override System.Collections.IEnumerator LogicalChildren
        {
            get
			{
				System.Collections.IEnumerator enumerator = this.GetLocalValueEnumerator();
				if (enumerator != null)
				{
					while (enumerator.MoveNext())
					{
						yield return enumerator.Current;
					}
				}
				foreach (object current in this.logicalChildren.Keys)
				{
					yield return current;
				}
				yield break;
			}
        }
        static UniversalControl()
        {
            System.Type typeFromHandle = typeof(UniversalControl);
            UniversalControl.IsBrowserHostedProperty = DependencyProperty.Register("IsBrowserHosted", typeof(bool), typeFromHandle, new PropertyMetadata(UniversalControl.GetIsBrowserHosted()));
        }
        public UniversalControl()
        {
            base.Loaded += new System.Windows.RoutedEventHandler(this.OnLoaded);
            base.Unloaded += new System.Windows.RoutedEventHandler(this.OnUnloaded);
            this.GoToStateCore(false);
        }
        public static bool GetIsBrowserHosted()
        {
            return System.Windows.Interop.BrowserInteropHelper.IsBrowserHosted;
        }
        public sealed override void BeginInit()
        {
            base.BeginInit();
            this.BeginInitCore();
        }
        public sealed override void EndInit()
        {
            this.EndInitCore();
            base.EndInit();
       } 
        protected virtual void BeginInitCore()
        {
        }
        protected virtual void EndInitCore()
        {
        }
        public sealed override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.OnApplyTemplateOverride();
            if (this.Initialize != null)
            {
                this.Initialize(this, System.EventArgs.Empty);
            }
        }
        protected virtual void OnApplyTemplateOverride()
        {
        }
        protected virtual void OnLoaded(object sender, System.Windows.RoutedEventArgs e)
        {
            this.GoToStateCore(false);
            if (!this.onLoadedInvoked)
            {
                this.onLoadedInvoked = true;
                this.RaiseActualLoaded();
            }
        }
        protected virtual void OnUnloaded(object sender, System.Windows.RoutedEventArgs e)
        {
        }
        protected virtual void RaiseActualLoaded()
        {
            if (this.ActualLoaded != null)
            {
                this.ActualLoaded(this, System.EventArgs.Empty);
            }
        }
        protected static DependencyProperty RegisterContentProperty(string name, System.Type propertyType, System.Type ownerType, object defaultValue, PropertyChangedCallback changedCallback)
        {
            PropertyChangedCallback propertyChangedCallback = delegate(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                UniversalControl universalControl = (UniversalControl)d;
                object oldValue = e.OldValue;
                object newValue = e.NewValue;
                if (oldValue != null)
                {
                    if (universalControl.logicalChildren.ContainsKey(oldValue))
                    {
                        universalControl.logicalChildren.Remove(oldValue);
                    }
                    universalControl.RemoveLogicalChild(oldValue);
                }
                if (newValue != null)
                {
                    universalControl.AddLogicalChild(newValue);
                    universalControl.logicalChildren.Add(newValue, true);
                }
                if (changedCallback != null)
                {
                    changedCallback(d, e);
                }
            };
            return DependencyProperty.Register(name, propertyType, ownerType, new PropertyMetadata(defaultValue, propertyChangedCallback));
        }
        protected void GoToState(bool useTransitions)
        {
            if (base.IsLoaded)
            {
                this.GoToStateCore(useTransitions);
            }
        }
        protected virtual void GoToStateCore(bool useTransitions)
        {
        }
    }
}